<?php
declare(strict_types=1);

namespace Service\AuthService;

use Service\BaseService;
use Service\Code\Normal;
use Service\ConfigureService\ChannelConfigService;
use Service\VenderClient\IdCardAuth\IdCardAuthClient;

/**
 * 二要素验证服务中心
 */
class IdCardAuthService extends BaseService
{
    /**
     * 验证二要素
     * @param array $data
     */
    public function checkIdCard(array $data = [])
    {
        $cid = $data['cid'];
        $result = ChannelConfigService::instance()->getConfig(['code' => 'id_auth','cid'=>$cid]);
        if ( $result['code'] == Normal::SUC && $result['data']['status'] == 1 )
        {
            return (new IdCardAuthClient($result['data']['value']))->check([
                "true_name" => $data['true_name'],
                "idcard"    => $data['idcard']
            ]);
        }
        return Normal::returnCode(Normal::FAIL,"验证失败,二要素未配置");

    }

    /**
     * 根据身份证获取性别
     */
    public function get_sex($idcard){
        if(empty($idcard)) return 0;
        $sexint=(int)substr($idcard,16,1);
        return $sexint % 2=== 0?2:1;
    }

    /**
     * 根据身份证获取生日
     */
    public function get_birthday($idcard)
    {
        if(empty($idcard)) return '';
        $bir=substr($idcard,6,8);
        $year=(int)substr($bir,0,4);
        $month=(int)substr($bir,4,2);
        $day=(int)substr($bir,6,2);
        return  $year."-".$month."-".$day;
    }

}